package com.abel.bigwater.tlv.model

import com.alibaba.fastjson.JSON
import io.netty.buffer.Unpooled
import org.junit.Test
import org.slf4j.LoggerFactory

class TlvDevPacketTest {
    private val log = LoggerFactory.getLogger(TlvDevPacketTest::class.java)

    @Test
    fun testOnline() {
        val dp = TlvDevPacket()
        dp.body = TlvDevBody(command = TlvCmdType.T1Online.cmd, dataList = ArrayList())
        dp.imei = "123456789ABCDEF"

        val d1 = TlvData(tag = BaseParam.TAG)
        val d2 = TlvData(tag = RealtimeParam.TAG)
        dp.body?.dataList?.add(d1)
        dp.body?.dataList?.add(d2)

        // tag=1
        val p11 = TlvDataPiece(d1.tag, BaseParam.T3MeterCode.id, TlvHelper.toByteArray("11 22 33 44 AA BB 55"))
        d1.pieceList?.add(p11)

        // tag = 2
        val p21 = TlvDataPiece(d2.tag, RealtimeParam.T7Pulse.id)
        p21.writeShort(1000)
        d2.pieceList?.add(p21)


        log.info("hex string: ${TlvHelper.toHexString(dp.fill(Unpooled.buffer()).let {
            val ba = ByteArray(it.readableBytes())
            it.readBytes(ba)
            ba
        })}")
    }

    @Test
    fun testOnlineParse() {
        val ba = TlvHelper.toByteArray("""68 38 36 33 37 30 33 30 33 33 36 33 32 34 36 39
            6f 76 62 3e f6 18 c4 fb 84 93 3b 0b d5 bf 59 e5 95 9e c8 da 18 98 cf e6 0f 05 0f 71 c7 72 0d aa 40 97 20 b2 e5 d7 a1 7a bd 16 40 da 09 09 1d 69 15 54 b9 da af 98 3f 11 15 74 0e f3 8d 0f 25 7e e2 9d 17 f1 e5 be cc c4 ca d0 1e ed 99 c9 e9 0c 4f 22 0c 1a 96 c9 ac d1 40 a1 38 33 93 2e 7e 61 42 2c 33 ff 6f c4 74 4e fb 48 bf eb 91 50 05 d0 42 2c 33 ff 6f c4 74 4e fb 48 bf eb 91 50 05 d0 42 2c 33 ff 6f c4 74 4e fb 48 bf eb 91 50 05 d0
             d4 16 """)

        val dp = TlvDevPacket().also { it.parse(Unpooled.buffer().writeBytes(ba)) }

        log.info(JSON.toJSONString(dp, true))
    }
}